<?php
/**
 * Created by PhpStorm.
 * Author: zhugege
 * Date: 2018/3/7
 * Time: 16:30
 */
namespace App\Http\Admin\Personnel\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use Illuminate\Http\Request;
use App\Http\Admin\Personnel\Models\Train;
use App\Api\Foundation\Upload\Models\ImageUpload;

class TrainController extends Controller
{
    /**
     * @author zhugege
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function planList()
    {
        try {
            if (request()->isMethod('post')) {
                $train = new Train();
                $trainList = $train->getTrainList();
                foreach ($trainList as $trainItem) {
                    $trainItem->start_time = Date('Y-m-d', $trainItem->start_time);
                    $trainItem->end_time = Date('Y-m-d', $trainItem->end_time);
                }
                return $trainList->toJson();
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        return $this->view('plan-list');
    }

    /**
     * 添加培训计划
     * @author zhugege
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function planAdd(Request $request)
    {
        try {
            if ($request->isMethod('post')) {
                $params = $this->getRequestParameters(['name', 'start_time', 'end_time', 'money', 'number', 'display', 'main_image_url']);
                $params['start_time'] = strtotime($params['start_time']);
                $params['end_time'] = strtotime($params['end_time']);
                $train = new Train();
                $result = $train->addTrain($params);
                return $result ? $this->ajaxSuccess('添加成功', [
                    'dataGridId' => 'train-plan-list'
                ]) : $this->ajaxFail();
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        return $this->view('plan-add');
    }

    /**
     * 编辑
     * @author zhugege
     */
    public function planEdit(Request $request)
    {
        try {
            $id = $request->id;
            $train = new Train();
            if ($request->isMethod('post')) {
                $params = $this->getRequestParameters(['name', 'start_time', 'end_time', 'money', 'number', 'display', 'main_image_url']);
                $params['start_time'] = strtotime($params['start_time']);
                $params['end_time'] = strtotime($params['end_time']);
                $result = $train->updateTrain($id, $params);
                return $result ? $this->ajaxSuccess('编辑成功', [
                    'dataGridId' => 'train-plan-list'
                ]) : $this->ajaxFail();
            }
            $trainInfo = $train->getTrainInfo($id)->toArray();

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        return $this->view('plan-edit', ['trainInfo' => $trainInfo]);
    }

    /**
     * 上传图片
     * @author zhugege
     * @return array
     */
    public function planImageUpload()
    {
        $img = request()->file('file');
        $imageUpload = new ImageUpload();
        $fileInfo = $imageUpload->upload($img, 'plan-image-upload', null, true);
        \DebugBar::disable();
        return [
            'statusCode' => 200,
            'filename' => $fileInfo['path']
        ];
    }

    /**
     * 培训报名列表
     * @author zhugege
     */
    public function trainApply()
    {
        try {
            if (request()->isMethod('post')) {
                $train = new Train();
                $params = $this->getRequestParameters(['pageCurrent', 'pageSize']);
                $trainApplyList = $train->getTrainApplyList($params['pageCurrent'], $params['pageSize']);

                foreach ($trainApplyList as $trainApplyItem) {
                    $trainApplyItem->apply_time = $trainApplyItem['created_at']->format('Y-m-d');
                }

                return $trainApplyList->toJson();
            }

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }

        return $this->view('train-apply');
    }

    /**
     * 报名详情
     * @author zhugege
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function trainApplyDetail()
    {
        $params = $this->getRequestParameters(['id']);
        $train = new Train();
        $trainApplyInfo = $train->getTrainApplyInfo($params['id']);
        $trainInfo = $train->getTrainInfo($params['id']);
        return $this->view('train-apply-detail', [
            'trainApplyInfo' => $trainApplyInfo,
            'trainInfo' => $trainInfo
        ]);
    }

    /**
     * 作业提交列表
     * @author zhugege
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function homeworkSubmitList()
    {
        try {
            if (request()->isMethod('post')) {
                $params = $this->getRequestParameters(['pageCurrent', 'pageSize']);
                $train = new Train();
                $homeworkSubmitList = $train->getHomeworkSubmitList($params['pageCurrent'], $params['pageSize']);
                return $homeworkSubmitList[0]->toJson();
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        return $this->view('homework-submit-list');
    }

    /**
     * 作业详情
     * @author zhugege
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function homeworkSubmitDetail()
    {
        $params = $this->getRequestParameters(['id']);
        $train = new Train();
        $homeworkSubmitInfo = $train->getHomeworkSubmitInfo($params['id']);
        return $this->view('homework-submit-detail', [
            'homeworkSubmitInfo' => $homeworkSubmitInfo
        ]);
    }

}